package com.qf.controller;

import com.qf.constatns.AuthConstatns;
import com.qf.entity.OrderEntity;
import com.qf.feign.api.AddressService;
import com.qf.feign.api.CarService;
import com.qf.interceptor.DynamicDataSourceInterceptor;
import com.qf.service.OrderService;
import com.qf.vo.Address;
import com.qf.vo.Customer;
import com.qf.vo.OrderParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Controller
@RequestMapping("/orderController")
@Slf4j
public class OrderController {

    @Autowired
    private CarService carService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private DynamicDataSourceInterceptor dynamicDataSourceInterceptor;

    @GetMapping("/{page}.html")
    public String toPage(@PathVariable String page, String gid, HttpSession session, ModelMap modelMap) {

        // 1.获取用户勾线的购物车
        List<String> carGidArray = new ArrayList<>();
        if (!StringUtils.isEmpty(gid)) {
            String[] split = gid.split(",");
            carGidArray = Arrays.asList(split);
        }

        // 2、获取当前等的用户
        Customer customer = (Customer) session.getAttribute(AuthConstatns.AUTH_SESSION);

        if (customer == null) {
            // 没有登录就跳转到登录页面
            return "redirect:http://localhost/auth/login.html";
        }

        // 3.根据用户id查询收货地址
        List<Address> addressList = addressService.getAddressListByUid(customer.getId());
        log.debug("addressList:{}", addressList);

        // 4.查询用户的购物车
        Map<String, Object> carMap = null;
        if (carGidArray.size() > 0) {
            carMap = carService.getCarListByUidAndGid(customer.getId(), carGidArray);
        } else {
            carMap = carService.getCarListByUid(customer.getId());
        }
        log.debug("{}", carMap);

        modelMap.put("addressList", addressList);
        modelMap.put("carMap", carMap); // ${carMap.carList} ${carMap.totalPrice}

        return page;
    }

    @RequestMapping("/createOrder")
    public String createOrder(OrderParam orderParam, HttpSession session) throws ExecutionException, InterruptedException {

        log.debug("【OrderController】当前线程id:{}", Thread.currentThread().getId());
        log.debug("orderParam:{}", orderParam);
        // 1.获取当前登录的用户
        Customer customer = (Customer) session.getAttribute(AuthConstatns.AUTH_SESSION);

        // 2.判断用户是否登录
        if (customer == null) {
            return "redirect:http://localhost/auth/login.html";
        }

        // 3.创建一个订单
        String oid = orderService.createOrder(orderParam, customer.getId());
        return "redirect:http://localhost/pay/ali/pay?oid=" + oid;
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public List<OrderEntity> list() {
        List<OrderEntity> list = (List<OrderEntity>) orderService.list();
        return list;
    }

    @RequestMapping("/getOrderById/{oid}")
    @ResponseBody
    public OrderEntity getOrderById(@PathVariable("oid") String oid) {
        Integer tabIndex = dynamicDataSourceInterceptor.getTabIndex();
        log.debug("tabIndex:{}", tabIndex);
        OrderEntity orderById = (OrderEntity) orderService.getOrderById(oid, tabIndex);
        return orderById;
    }

    @RequestMapping("/updateOrderStatus")
    @ResponseBody
    void updateOrderStatus(@RequestParam("oid") String oid, @RequestParam("status") Integer status) {
        // 3.修改订单的状态
        orderService.updateOrderStatus(oid, status);
    }

    // 这个接口要返回一个order对象，里面还要把保存订单详情
    @RequestMapping("/getOrderInfoById/{oid}")
    @ResponseBody
    public OrderEntity getOrderInfoById(@PathVariable("oid") String oid){
        return orderService.getOrderInfoById(oid);
    }

}
